//
//  ParentController.m
//  DZbbs2
//
//  Created by zengyuan on 7/31/13.
//  Copyright (c) 2013 zengyuan. All rights reserved.
//

#import "ParentController.h"
#import "AppDelegate.h"

#import "UserUnit.h"
//#import "MMDrawerController.h"
#import "MLNavigationController.h"
@interface ParentController ()

@end

NSString* g_sessionId = nil;

@implementation ParentController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor=MAIN_DARKCOLOR;
    
    if(OSVersionIsAtLeastiOS7){
        self.navigationController.navigationBar.barTintColor=NAV_COLOR;
        self.navigationController.navigationBar.translucent = NO;
        
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    }
    
    else{
        if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
        {
            //if iOS 5.0 and later
            UIEdgeInsets insets = UIEdgeInsetsMake(2, 2,2, 2);
            UIImage *streImage = [[UIImage imageNamed:@"navbk"] resizableImageWithCapInsets:insets];
            
            [self.navigationController.navigationBar setBackgroundImage:streImage forBarMetrics:UIBarMetricsDefault];
        }
        
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
        
    }

//    [self hideNavBottomBlackLine];

    [self setNavbarItem];

}

-(void)removeNavPanGesture{
    if ([self.navigationController respondsToSelector:@selector(removeNavPanGesture)]) {
        [(MLNavigationController *)(self.navigationController) removeNavPanGesture];
    }
}

-(void)addNavPanGesture{
    if ([self.navigationController respondsToSelector:@selector(addNavPanGesture)]) {
    [(MLNavigationController *)(self.navigationController) addNavPanGesture];
            }
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self addNavPanGesture];
    
}

-(BOOL)networkAvailble{
    
    Reachability*rea= [Reachability reachabilityForInternetConnection];
    return [rea isReachable];
//    if (![rea isReachable]){
////        MBALERT(HTTPERROR_CANNOTREACH);
//        return;
//    }
    
}

/*
    隐藏导航条底下的黑线
 */

-(void)hideNavBottomBlackLine{
    if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
        
        NSArray *list=self.navigationController.navigationBar.subviews;
        
        for (id obj in list) {
            if ([obj isKindOfClass:[UIImageView class]]) {
                UIImageView *imageView=(UIImageView *)obj;
                imageView.hidden=YES;
            }
            
        }
        UIEdgeInsets insets = UIEdgeInsetsMake(2, 2,2, 2);
        UIImage *streImage = [[UIImage imageNamed:@"navbk"] resizableImageWithCapInsets:insets];
        
        UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, -20, 420, 64)];
        imageView.image=streImage;
        [self.navigationController.navigationBar addSubview:imageView];
        [self.navigationController.navigationBar sendSubviewToBack:imageView];
    }
}

-(void)addReachabilityChangedNotifi{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reachabilityChanged:)
                                                 name:kReachabilityChangedNotification
                                               object:nil];
    
    networkReach = [Reachability reachabilityWithHostname:@"www.baidu.com"];
    [networkReach startNotifier];
}

-(void)removeReachabilityChangedNotifi{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
    
    [networkReach stopNotifier];

}

-(void)reachabilityChanged:(NSNotification*)note{
    Reachability * reach = [note object];
    

    if (reach == networkReach) {
        if([reach isReachable]){
            NSLog(@"isReachable ");
        }else{
            NSLog(@"not reachable");
        }
    }
}


-(void)setNavbarItem
{
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController setNavigationBarHidden:NO];
    
    self.navigationController.navigationBar.backItem.hidesBackButton = YES;
    [self.navigationItem setHidesBackButton:YES];
 
    self.backBtnPar=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.backBtnPar setFrame:CGRectMake(0, 0, 40, 40)];
    if(OSVersionIsAtLeastiOS7){
        [self.backBtnPar setImageEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 0)];
    }
    [self.backBtnPar  addTarget:self action:@selector(navLeftAction) forControlEvents:UIControlEventTouchUpInside];
    [self.backBtnPar setImage:[UIImage imageNamed:@"common_backbtn"] forState:UIControlStateNormal];
    [self.backBtnPar setImage:[UIImage imageNamed:@"common_backbtn_h"] forState:UIControlStateHighlighted];
    
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:self.backBtnPar];
    self.navigationItem.leftBarButtonItem = leftItem;
    //back button
    if ([self.navigationController.viewControllers count]>1) {
        self.backBtnPar.hidden=NO;
    }else{
        self.backBtnPar.hidden=YES;

    }
    
    //right button
    self.rightBtnPar = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.rightBtnPar setFrame:CGRectMake(0, 0, 40,40)];
    [self.rightBtnPar.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [self.rightBtnPar setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

//    [self.rightBtnPar.titleLabel setMinimumFontSize:8.];

    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightBtnPar];
    self.navigationItem.rightBarButtonItem = rightItem;
    [self.rightBtnPar addTarget:self action:@selector(navRightAction) forControlEvents:UIControlEventTouchUpInside];

 
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 30)];
    self.titleLabel.font = NAVBAR_FONT;
 
    self.titleLabel.textColor=[UIColor whiteColor];
    self.titleLabel.backgroundColor=[UIColor clearColor];

    self.navigationItem.titleView=self.titleLabel;
    self.titleLabel.textAlignment=NSTextAlignmentCenter;

    //设置导航栏标题的属性 行间距、字号、颜色
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing=0.9;
    
    /*
     if (OSVersionIsAtLeastiOS6) {
        NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                [UIColor whiteColor],NSForegroundColorAttributeName,
                [UIFont systemFontOfSize:22],NSFontAttributeName,
                NSParagraphStyleAttributeName,paragraphStyle,
                                           nil];
        
        if (!OSVersionIsAtLeastiOS7) {
            [attributes setObject:[UIColor clearColor] forKey:NSShadowAttributeName];
        }
        [self.navigationController.navigationBar setTitleTextAttributes:attributes];
     }

    */
    
    self.navigationItem.title=@"";

 }

-(void)navLeftAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)navRightAction{
    

}

-(void)destoryMemBeforePop{
    
}



-(void)setNavbarImage{
    UIImageView *imgview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"顶部Logo"]];
    imgview.frame=CGRectMake(0, 0, 126, 30);
    self.navigationItem.titleView=imgview;
}
-(void)setNavbarTitle:(NSString *)text{
    
    if ([text length]) {
        self.navigationItem.titleView=nil;
        self.navigationItem.title=text;
    }else{
        [self setNavbarImage];
    }

}

-(void)goback{
    [self.navigationController popViewControllerAnimated:YES];
}

//6.0
- (BOOL) shouldAutorotate {
    return YES;
}

//6.0
- (NSUInteger)supportedInterfaceOrientations
{
    return  UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
//- (dispatch_queue_t)serialQueue
//{
//    if (!_serialQueue) {
//        _serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);//创建串行队列
//    }
//    return _serialQueue;
//}
//
//- (void)loginDispath
//{
//    dispatch_async([self serialQueue], ^{
//        
//        NSLog(@"--------loginDispath");
//
////        UserUnit *unit = [UserUnit getActiveUser];
////        int tag = [ZhongweiLib ParentLogin:unit.userName passwd:unit.userPasswd];
////        if (tag==0) {
////            OnlineEngine *onlineEngine = [OnlineEngine sharedInstance];
////            [onlineEngine initWork];
////            onlineEngine.delegate=self;
////        }
//    });
//}

/*
-(void)postDeviceInfoDispatch{
    UserUnit *unit = [UserUnit getActiveUser];
    NSString *deviceudid =[[[UIDevice currentDevice]identifierForVendor] UUIDString];
    NSString *sys =[[UIDevice currentDevice]systemVersion];
    
    NSString *appversion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    
    NSString *deviceVer = [NSString stringWithFormat:@"%@", [[UIDevice currentDevice]model]];
    ;
    
    NSString *request = [NSString stringWithFormat:@"{\"%@\":%d,\"%@\":\"%@\",\"%@\":\"%@\",\"%@\":\"%@\",\"%@\":\"%@\",\"%@\":\"%@\",\"%@\":\"%@\"}", JK_MESSAGE_TYPE,REPORT_PLATFORM_INFO,
                         JK_USERNAME,unit.userName,
                         JK_MOBILE_IMEI,deviceudid,
                         JK_MOBILE_MAC,@"",
                         JK_MOBILE_SYS,sys,
                         JK_MOBILE_BRAND,deviceVer,
                         JK_VERSION_NAME, appversion];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString *response;
        [ZhongweiLib GetResponseByRequestShortConnectionServer:JK_USER_INFO_PROCESS request:request response:&response];
        
    });
    
    
    
}


-(void)onlineDispatch{
    DLog();
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        OnlineEngine *onlineEngine = [OnlineEngine sharedInstance];
        [onlineEngine regServerOnline];
        onlineEngine.delegate=self;
        
    });

}
*/

//- (void)requestBabyInfoDispath
//{
//    dispatch_async([self serialQueue], ^{
//        NSLog(@"--------requestBabyInfoDispath");
//          });
//}

-(void)addTwoLinesFor:(UIView *)view{
    HorizonLine *line = [[HorizonLine alloc]initWithFrame:CGRectMake(0, 0, view.frame.size.width, 1)];
    [view addSubview:line];
    [line setLineColor:LINE_COLOR];
    
    line = [[HorizonLine alloc]initWithFrame:CGRectMake(0, view.frame.size.height-0, view.frame.size.width, 1)];
    [view addSubview:line];
    [line setLineColor:LINE_COLOR];

}

-(void)setBtnGreenBackgroundImage:(UIButton *)btn{
    
//    UIImage *newBtnImage = [UIImage resizableImage:@"圆角矩形-3"];
//    [];
//    [btn setBackgroundImage:newBtnImage forState:UIControlStateNormal];
    
    [btn setBackgroundColor:HexRGB(0x7CBB1E)];
}


-(void)setBtnDisabledDarkBackgroundImage:(UIButton *)btn{
    UIImage *streImage = [[UIImage imageNamed:@"redbtn_bk_disabled"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 80, 0, 80)];
    [btn setBackgroundImage:streImage forState:UIControlStateNormal];
    btn.userInteractionEnabled=NO;
}

-(void)addFullloadView{
    
    if (!fullloadingView) {
        CGRect rect= KEY_WINDOW.bounds;
        rect.origin.x=0;
        rect.origin.y=0;
        rect.size.height=rect.size.height-self.navigationController.navigationBar.frame.size.height-(OSVersionIsAtLeastiOS7?20:0);
        
        fullloadingView = [[FullLoadingView alloc]initWithFrame:rect];
        [self.view addSubview:fullloadingView];
    }
    
}

-(void)dismissFullloadingView{
    
    if(fullloadingView){
        //删除等待
        [UIView animateWithDuration:0.3 animations:^{
            fullloadingView.alpha=0;
        } completion:^(BOOL finished) {
            [fullloadingView removeFromSuperview];
            fullloadingView=nil;
        }];
    }
    
}

-(void)showErrorTipMsgInViewController:(NSString *)err{
    if (fullloadingView) {
        [fullloadingView showErrorMsg:err];
    }else{
        MBALERT(err);
    }
}


+(id)fromSB:(NSString *)sbid{
 
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [board instantiateViewControllerWithIdentifier:sbid];
    
    return vc;
}



@end
